<?php 
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Session;
class Train extends Controller
{
	public function index(){
        $session = Session::get('admin');
    if (!empty($session)) {
    $train=Db::name("train")->select();
        //dump($activity);exit;
        $this->assign("train",$train);
        return $this->fetch(); 
    }
    else{
        $this->error('您没有权限!','train/login/index');
    }

	}
	public function delete($id){
		//先把对应的用户头像删除
		$train=Db::name("train")->find($id);
		// dump(Db::name("users")->getLastSQL());
		// dump($user);exit;
		$pic=$train["train_pic"];
		$file=ROOT_PATH."public".DS."static".DS."admin".DS."img".DS.$pic;
		if(file_exists($file))unlink($file);

		if(Db::name("train")->delete($id))
			$this->success('删除成功');
		else $this->error('删除失败');
    }
    

	public function create(){
        return $this->fetch('create');
    }
    public function add(){
    	$file = request()->file('train_pic');
  
    // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
             $info = $file->rule('uniqid')->validate(['size'=>3000000,'ext'=>'jpg,png,gif,jpeg'])->move(ROOT_PATH . 'public' . DS . 'static'.DS.'admin'. DS . 'img');
         // ;exit;
        if($info){
                $pic=$info->getFileName();
               // dump($pic);exit();
        }else{
            // 上传失败获取错误信息
            echo $file->getError();
        }
    }

    	//获取表单数据
    	//$pname=$_POST["pname"];

        $data=[
        	'train_name'=>input('train_name'),
        	'train_intro'=>input('train_intro'),
        	'train_classtime'=>input('train_classtime'),
        	'train_place'=>input('train_place'),
        	'train_konw'=>input('train_konw'),
        	'train_price'=>input('train_price'),
        	'train_pic'=>empty($pic)?"slsod.jpg":$pic
        ];
    	//存入数据库
    	if(DB::name('train')->insert($data)){    		
    		//dump($data);exit;
    		$this->success('添加成功','admin/train/index');

    	}else{
    		$this->error('添加失败','admin/train/create');
    	}
    }
     public function edit(){
        //获取视频类型列表
        $train_id=input('train_id');
        //dump($activity_id);
       $train=Db::name("train")->where('train_id',$train_id)->find();
        // dump($train);exit;
        $this->assign("train",$train);
        //dump($user);
        return $this->fetch('edit');
    }
  
    public function update(){

        //图片是否修改

        //获取其他的表单数据

        $train_id=input('train_id');
        //在数据库里修改
        $file = request()->file('train_pic');
        //dump($uid);
    // 移动到框架应用根目录/public/uploads/ 目录下
    if($file){
        //dump($file);
        $info = $file->rule('uniqid')->validate(['size'=>3000000,'ext'=>'jpg,png,gif,jpeg'])->move(ROOT_PATH . 'public' . DS . 'static'.DS.'admin'. DS . 'img');
         // ;exit;
        if($info){
                $pic=$info->getFileName();
               //dump($pic);
        }else{
            // 上传失败获取错误信息
            echo $file->getError();
        }
    }
        $data=[
            'train_name'=>input('train_name'),
            'train_intro'=>input('train_intro'),
            'train_classtime'=>input('train_classtime'),
            'train_place'=>input('train_place'),
            'train_konw'=>input('train_konw'),
            'train_price'=>input('train_price'),
            'train_pic'=>$pic,
        ];
        // dump($data);
        // exit();
        //存入数据库
        if(DB::name('train')->where('train_id',$train_id)->update($data)){            
        
            $this->success('修改成功','admin/train/index');

        }else{
            $this->error('修改失败','admin/train/edit');
        }
    }
    public function search(){
        $content=input('content');
        $train=Db::table('train')->where('train_name','like',$content)->select();
        $this->assign("train",$train);

        return $this->fetch('index');
       // dump($sql);
    }

}
 ?>